package hrms.service;

import org.springframework.stereotype.Service;

import hrms.util.Result;
import hrms.util.amq.Consumer;
import hrms.util.amq.Producter;
import hrms.util.amq.TopicPublisher;
import hrms.util.amq.TopicSubscriber;

@Service("amqService")
public class AmqServiceImpl implements AmqService{

	public Result<Object> sendToAmq(String disname ,String mes) {
		Result<Object> result = new Result<Object>();
		Producter tProducter = Producter.getInstance();
		
		if(!tProducter.sendMessage(disname, mes)) {
			result.setStatus(1);
			result.setMsg("消息存入MQ失败!");
		}
		result.setStatus(0);
		result.setMsg("消息存入MQ成功!");
		return result;
	}
	
	public Result<Object> recFromAmq(String disname) {
		Result<Object> result = new Result<Object>();
		Consumer tComsumer = Consumer.getInstance();
		
		if(!tComsumer.getMessage(disname)) {
			result.setStatus(1);
			result.setMsg("MQ消息读取失败:"+tComsumer.error);
		}
		result.setStatus(0);
		result.setMsg("MQ消息读取成功!");
		result.setData(tComsumer.mes);
		return result;
	}

	public Result<Object> recAll(String disname) {
		Result<Object> result = new Result<Object>();
		Consumer tComsumer = Consumer.getInstance();
		
		if(!tComsumer.getMessageListener(disname)) {
			result.setStatus(1);
			result.setMsg("MQ消息读取失败:"+tComsumer.error);
		}
		result.setStatus(0);
		result.setMsg("MQ消息读取成功!");
		result.setData(tComsumer.list);
		return result;
	}
	
	public Result<String> sendTopic(String text){
		Result<String> result = new Result<String>();
		TopicPublisher tTopicPublisher = new TopicPublisher();
		if(!tTopicPublisher.sendMessage(text)) {
			result.setStatus(1);
			result.setMsg("发布订阅失败！");
			return result;
		}
		result.setStatus(0);
		result.setMsg("发布订阅成功！");
		return result;
	}
	
	
	public Result<String> recTopic(String adminCode){
		Result<String> result = new Result<String>();
		TopicSubscriber tTopicSubscriber = new TopicSubscriber(adminCode);
		String mes = tTopicSubscriber.mes;
		result.setStatus(0);
		result.setData(mes);
		return result;
	}
}
